package br.com.ss.portal.controller.responsavel;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import br.com.ss.portal.model.dao.ResponsavelDao;
import br.com.ss.portal.model.entity.Responsavel;

@Component
public class ResponsavelController {

	private static final Logger logger = LoggerFactory
			.getLogger(ResponsavelController.class);

	private Responsavel responsavel = new Responsavel();
	private List<Responsavel> responsaveis;
	private Responsavel selected;

	@Autowired
	private ResponsavelDao responsavelDao;

	public String getMessage() {
		logger.debug("Returning message from responsavel home bean");
		return "Hello from Spring";
	}

	public Responsavel getResponsavel() {
		return responsavel;
	}

	public void save() {
		
		if (this.responsavel.getId() == 0)
			responsavelDao.save(this.responsavel);
		else
			responsavelDao.merge(this.responsavel);
		this.responsavel = new Responsavel();
		this.responsaveis = responsavelDao.list();

	}

	public String listAll() {
		this.responsaveis = responsavelDao.list();
		return "greeting";
	}
	
	public void editar() {
		this.responsavel = this.selected;
		this.selected = new Responsavel();
	}
	
	public void remove(){
		this.responsavel = this.selected;
		responsavelDao.remove(this.responsavel);
		this.responsaveis = responsavelDao.list();
	}

	public List<Responsavel> getResponsaveis() {
		if (this.responsaveis == null)
			this.responsaveis = responsavelDao.list();

		System.out.println("this.responsaveis si ze "
				+ this.responsaveis.size());
		return this.responsaveis;
	}

	public void setResponsaveis(List<Responsavel> responsaveis) {
		this.responsaveis = responsaveis;
	}

	public Responsavel getSelected() {
		return selected;
	}

	public void setSelected(Responsavel selected) {
		this.selected = selected;
	}

	public void setResponsavel(Responsavel responsavel) {
		this.responsavel = responsavel;
	}

}